package com.example.libowu.musicplayer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by libowu on 17-12-9.
 */

public class MusicImg extends SurfaceView implements SurfaceHolder.Callback ,Runnable{
    private Bitmap bitmap;
    private SurfaceHolder surfaceHolder;
    private Canvas canvas;
    private Paint paint;
    private int rotate=0;
    private boolean isStart=true;
    private float x;
    private boolean isWait=false;

    public MusicImg(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint=new Paint();
        surfaceHolder=getHolder();
        surfaceHolder.addCallback(this);
        bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.music);
        bitmap=Bitmap.createScaledBitmap(bitmap,910,910,true);
        setZOrderOnTop(true);
        getHolder().setFormat(PixelFormat.TRANSLUCENT);
        paint.setColor(Color.RED);
    }


    //当用户点击home键时surfaceview将会被调用,此时canvas将为空值,当销毁时isStart将被这是成false,为了在返回活动时无法执行run,因在surfaceCreated()里将isStart设置成true;
    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        Log.e("启动create","create启动");
        new Thread(this).start();
        isStart=true;
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        isStart=false;
        Log.e("surfaceview销毁","启动销毁");
    }

    public void paint(){
        try{
            canvas=surfaceHolder.lockCanvas();
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
            //canvas.translate(x,0);
            canvas.rotate(rotate,455,455);
            canvas.drawBitmap(bitmap,getWidth()/2-455,getHeight()/2-455,paint);
            //canvas.drawCircle(getHeight()/2,getWidth()/2,getWidth()/2,paint);
            surfaceHolder.unlockCanvasAndPost(canvas);
        }catch (NullPointerException e){
            Log.e("空指针异常","异常出现");
        }
    }

    @Override
    public void run() {
        synchronized (this){

            while (isStart){
                if(isWait){
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                rotate=rotate+2;
                paint();
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void setStart(boolean isStart){
        this.isStart=isStart;
    }
    public void setWait(boolean isWait){
        this.isWait=isWait;
    }
    public void setNotific(){
        synchronized (this){
            this.notify();
            isWait=false;
        }
    }

    public boolean getWait(){
        return isWait;
    }

}
